/*
Author:tomson
Description:用于访问服务器
Date:2019-04-24
ver:1.0
  ServerUrl:'http://hgh.ezcsoft.com/',
  zcDomain : 'http://hgh.ezcsoft.com/vue-resource/vue-resource/',
  IsDebug:false,

发布http://zcsoft.s1.natapp.cc/zew/
ServerUrl:'http://zcsoft.s1.natapp.cc/',
zcDomain : 'http://zcsoft.s1.natapp.cc/vue-resource/vue-resource/',
IsDebug:false,
本地
ServerUrl:'http://127.0.0.1:8056/',
zcDomain : 'http://127.0.0.1:8087/vue-resource/',
IsDebug:true,
*/

//module.exports
var zcServer = {
  ServerUrl:'http://127.0.0.1:8056/',
  zcDomain : 'http://127.0.0.1:8087/vue-resource/',
  IsDebug:true,

  IsNeedLogin:'true',
  Get:function (options) {
    let me = this;
    if(me.IsDebug){
      me.getByJsonP(options);
    }
    else{
      me.getByNormal(options);
    }
  },
  Post:function (options) {
    let me = this;
    if(me.IsDebug){
      me.postByJsonP(options);
    }
    else{
      me.postByNormal(options);
    }
  },
  PostOss:function (options) {
    let me = this;
    if(me.IsDebug){
      me.postOssByJsonP(options);
    }
    else{
      me.postOssByNormal(options);
    }
  },
  DownOss:function(options){
    let me = this;
    window.location.href = me.ServerUrl + 'PageService/doOSSDownload.ashx?pSN=' + options.FileSN;
  },
  getByNormal:function (options) {
    let me = this;
    me.IsNeedLogin = options.IsNeedLogin || me.IsNeedLogin;
    let curServiceUrl = '';
    if(options.ServerUrl)
      curServiceUrl = me.ServerUrl + options.ServerUrl + '?IsNeedLogin=' + me.IsNeedLogin + '&dc='+me.getDc();
    else
      curServiceUrl = me.ServerUrl + 'PageService/doVueLocalService.ashx?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();

    options.Scope.$Global.http.post(curServiceUrl,options.Paras)
    .then(function(response){
      if(response.status === 200){
        if(options.Callback){
          options.Callback(response.data,options.Scope);
        }
      }
      else{
        options.Callback({
          success:false,
          msg:'网络问题，无法连接到服务器!'
        },options.Scope);
      }
    })
    .catch(function(err){
      options.Callback({
        success:false,
        msg:'异常错误，无法连接到服务器!'
      },options.Scope);
    });
  },
  getByJsonP:function (options) {
    let me = this;
    me.IsNeedLogin = options.IsNeedLogin || me.IsNeedLogin;
    let curServiceUrl = '';
    if(options.ServerUrl)
      curServiceUrl = me.ServerUrl + options.ServerUrl + '?IsNeedLogin=' + me.IsNeedLogin + '&dc='+me.getDc();
    else
      curServiceUrl = me.ServerUrl + 'PageService/doVueDebugService.ashx?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();

    options.Scope.$Global.http.post(curServiceUrl,JSON.stringify(options.Paras),{withCredentials:true})
    .then(function(response){
      if(response.status === 200){
        if(options.Callback){
          options.Callback(response.data,options.Scope);
        }
      }
      else{
        options.Callback({
          success:false,
          msg:'网络问题，无法连接到服务器!'
        },options.Scope);
      }
    })
    .catch(function(err){
      options.Callback({
        success:false,
        msg:'异常错误，无法连接到服务器!'
      },options.Scope);
    });
  },
  postByNormal:function (options) {
    let me = this;
    me.IsNeedLogin = options.IsNeedLogin || me.IsNeedLogin;
    let curServiceUrl = '';
    if(options.ServerUrl)
      curServiceUrl = me.ServerUrl + options.ServerUrl + '?IsNeedLogin=' + me.IsNeedLogin + '&dc='+me.getDc();
    else
      curServiceUrl = me.ServerUrl + 'PageService/doVueLocalService.ashx?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();

    options.Scope.$Global.http.post(curServiceUrl,options.Paras)
    .then(function(response){
      if(response.status === 200){
        if(options.Callback){
          options.Callback(response.data,options.Scope);
        }
      }
      else{
        options.Callback({
          success:false,
          msg:'网络问题，无法连接到服务器!'
        },options.Scope);
      }
    })
    .catch(function(err){
      options.Callback({
        success:false,
        msg:'异常错误，无法连接到服务器!'
      },options.Scope);
    });
  },
  postByJsonP:function (options) {
    console.log('postByJsonP999',options);
    let me = this;
    me.IsNeedLogin = options.IsNeedLogin || me.IsNeedLogin;
    let curServiceUrl = '';
    console.log('me.IsNeedLogin',me.IsNeedLogin);
    if(options.ServerUrl)
      curServiceUrl = me.ServerUrl + options.ServerUrl + '?IsNeedLogin=' + me.IsNeedLogin + '&dc='+me.getDc();
    else
      curServiceUrl = me.ServerUrl + 'PageService/doVueDebugService.ashx?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();

    options.Scope.$Global.http.post(curServiceUrl,JSON.stringify(options.Paras),{withCredentials:true})
    .then(function(response){
      console.log('response999',response);
      if(response.status === 200){
        if(options.Callback){
          options.Callback(response.data,options.Scope);
        }
      }
      else{
        options.Callback({
          success:false,
          msg:'网络问题，无法连接到服务器!'
        },options.Scope);
      }
    })
    .catch(function(err){
      console.log('zcServer.js-getByJsonP-catch:',err);
      options.Callback({
        success:false,
        msg:'异常错误，无法连接到服务器!'
      },options.Scope);
    });
  },
  postOssByNormal:function (options) {
    let me = this;
    me.IsNeedLogin = options.IsNeedLogin || me.IsNeedLogin;
    let curServiceUrl = '';
    if(options.ServerUrl)
      curServiceUrl = me.ServerUrl + options.ServerUrl + '?IsNeedLogin=' + me.IsNeedLogin + '&dc='+me.getDc();
    else
      curServiceUrl = me.ServerUrl + 'PageService/doOSSService.ashx?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();

    options.Source.Scope.$Global.http.post(curServiceUrl,options.Paras)
    .then(function(response){
      if(response.status === 200){
        if(options.Callback){
          options.Callback(response.data,{
            UserData:options.Source.UserData
          });
        }
      }
      else{
        options.Callback({
          success:false,
          msg:'网络问题，无法连接到服务器!'
        },options.Scope);
      }
    })
    .catch(function(err){
      options.Callback({
        success:false,
        msg:'异常错误，无法连接到服务器!'
      },options.Scope);
    });
  },
  postOssByJsonP:function (options) {
    let me = this;
    me.IsNeedLogin = options.IsNeedLogin || me.IsNeedLogin;
    let curServiceUrl = '';
    console.log('me.IsNeedLogin',me.IsNeedLogin);
    if(options.ServerUrl)
      curServiceUrl = me.ServerUrl + options.ServerUrl + '?IsNeedLogin=' + me.IsNeedLogin + '&dc='+me.getDc();
    else
      curServiceUrl = me.ServerUrl + 'PageService/doVueOSSDebugService.ashx?IsNeedLogin=' + me.IsNeedLogin + '&dc=' + me.getDc();
    console.log('curServiceUrl',curServiceUrl);
    options.Source.Scope.$Global.http.post(curServiceUrl,JSON.stringify(options.Paras),{withCredentials:true})
    .then(function(response){
      if(response.status === 200){
        if(options.Callback){
          options.Callback(response.data,{
            UserData:options.Source.UserData
          });
        }
      }
      else{
        options.Callback({
          success:false,
          msg:'网络问题，无法连接到服务器!'
        },options.Scope);
      }
    })
    .catch(function(err){
      options.Callback({
        success:false,
        msg:'异常错误，无法连接到服务器!'
      },options.Scope);
    });
  },
  getDc:function () {
    return new Date().getTime();
  }
}

export {zcServer}
/*


使用说明
<script>

this.$Global.zcServer.Post({
    IsNeedLogin:'false',//根据需求而定
    ServerUrl:'PageService/doVueLogin.ashx',//根据需求而定
    Paras:{
        Name:'VueTest',
        Action:'getOpenId',
        UserId:'A001',//根据需求而定
        Data:{
          Id:5
        }
    },
    Callback:me.Callback_GetOpenId,
    Scope:me
});
</script>
 */
